<?php

namespace app\common\model\order;

use app\common\model\Artificer;
use app\common\model\Config;
use fast\Random;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class Rescue extends Model
{
    use SoftDelete;

    protected $name = 'rescue_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    protected $insert = ['order_no'];

    // 追加属性
    protected $append = [
        'createtime_text',
        'status',
    ];

    public function getStatusAttr($value,$data)
    {
        //10 等待中，待分配   20 已分配，等待技工处理   30 技工接单，立即前往   40 技工到达现场   50:技工拍照上传，援助完成  60：用户支付后，订单完成
        $value = '10';
        if(isset($data['distribution_status']) && $data['distribution_status']){
            $value = $data['distribution_status'];
        }
        if(isset($data['take_status']) && ($data['take_status'] == '20')){
            $value = '30';
        }
        if(isset($data['reach_status']) && ($data['reach_status'] == '20')){
            $value = '40';
        }
        if(isset($data['aid_status']) && ($data['aid_status'] == '20')){
            $value = '50';
        }
        if(isset($data['pay_status']) && ($data['pay_status'] == '20')){
            $value = '60';
        }
        return $value;
    }

    public function getCreatetimeTextAttr($value,$data)
    {
        return $value ? $value : (isset($data['createtime']) ? date('Y-m-d H:i:s',$data['createtime']) : '');
    }

    protected function setOrderNoAttr($value){
        return date('ymdHis').Random::numeric();
    }

    public function artificer()
    {
        return $this->belongsTo(Artificer::class,'artificer_id','id',[],'left')->setEagerlyType(0);
    }

    /**
     * 设置错误信息
     * @param $msg
     */
    public function setError($msg)
    {
        $this->error = $msg;
    }

    /**
     * 获取错误信息
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    public function getPrice($distance)
    {
        //起步价格
        $initiate_price = Config::getValue('initiate_price','assistance');
        // 起步距离
        $starting_distance = Config::getValue('starting_distance','assistance');
        //超出部分单价
        $unit_price = Config::getValue('unit_price','assistance');
        $poor = ($distance > $starting_distance) ? $distance - $starting_distance : 0;
        $total_price = $initiate_price + $unit_price*$poor;
        return ['starting_price' => $initiate_price,'price' => $unit_price,'total_distance'=>$distance,'poor'=>$poor,'total_price'=>$total_price];
    }

    /**
     * 列表
     * @param $where
     * @param $limit
     * @param $page
     * @param $extend
     * @return false|\think\Paginator
     */
    public function list($where, $limit = 10, $page = 1, $extend=[]){
        try {
            $list = self::where($where)->field('id,order_no,lng,lat,address,describe,createtime,total_price,take_status,distribution_status,reach_status,order_status,pay_status,artificer_id,aid_status')->order('id desc')->paginate($limit,true,['page'=>$page])->each(function ($item){
                $item->artificerLng  = $item->artificer->lng;
                $item->artificerLat  = $item->artificer->lat;
                unset($item->artificer);
                //计算到技工的距离
                $item->distance = GetDistance($item->lat,$item->lng,$item->artificerLat,$item->artificerLng,2);
                return $item;
            });
            return  $list;
        }catch (Exception $e){
            $this->setError($e->getMessage());
            return false;
        }
    }

    /**
     * 援助订单详情
     * @param $id
     * @return Rescue|false
     */
    public function details($id)
    {
        try {
            $row = self::get($id);
            if(empty($row)){
                throw new Exception('参数错误');
            }
            $row->artificerLng  = $row->artificer->lng;
            $row->artificerLat  = $row->artificer->lat;
            unset($row->artificer);
            //计算到技工的距离
            $row->distance = GetDistance($row->lat,$row->lng,$row->artificerLat,$row->artificerLng,2);
            return  $row;
        }catch (Exception $e){
            $this->setError($e->getMessage());
            return false;
        }
    }


}